﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace AlgorithmStarter
{
    public partial class Form1 : Form
    {
        AlgorithmsManager algManager;
        CommunicationsManager comManager;
        DateTime starTime;
        public Form1()
        {
            InitializeComponent();
            comManager = new CommunicationsManager("127.0.0.1", 12345);
            comManager.ConnessioneStabilita += new CommunicationsManager.ConnessioneHandler(comManager_ConnessioneStabilita);
            comManager.ConnessioneChiusa += new CommunicationsManager.ConnessioneHandler(comManager_ConnessioneChiusa);
            comManager.RicevutoMessaggio += new CommunicationsManager.MessaggioHandler(comManager_RicevutoMessaggio);
            comManager.MettitiInAscolto();
            algManager = new AlgorithmsManager(comManager);
            algManager.OnIgnorePhaseCompleted += new AlgorithmsManager.IgnorePhaseCompletedDelegate(algManager_OnIgnorePhaseCompleted);
            algManager.OnRunPhaseCompleted += new AlgorithmsManager.RunPhaseCompletedDelegate(algManager_OnRunPhaseCompleted);
        }

        void comManager_RicevutoMessaggio(Message messaggio)
        {
            if (messaggio is SetAlgorithm)
            {
                labelText = "Ricevuto SetAlgorithm";
                algManager.CallSetAlgorithm(messaggio);
            }
            if (messaggio is Ping)
            {
                labelText = "Ricevuto Ping";
            }
            label1.Invoke(new SetlabelDelegate(SetLabelText));
        }

        void comManager_ConnessioneChiusa()
        {
            labelText = "Connessionbe Chiusa";
            label1.Invoke(new SetlabelDelegate(SetLabelText));
        }

        void comManager_ConnessioneStabilita()
        {
            labelText = "Connessione Stabilita";
            label1.Invoke(new SetlabelDelegate(SetLabelText));
        }

        string labelText = "";
        private void SetLabelText()
        {
            label1.Text = labelText;
            label1.Invalidate();
        }
        private delegate void SetlabelDelegate();

        void algManager_OnRunPhaseCompleted()
        {
            TimeSpan diff = DateTime.Now - starTime;
            labelText = String.Format("Run Phase Completed, took {0}sec {1}msec", diff.Seconds, diff.Milliseconds);
            label1.Invoke(new SetlabelDelegate(SetLabelText));
        }

        void algManager_OnIgnorePhaseCompleted()
        {
            TimeSpan diff = DateTime.Now - starTime;
            labelText = String.Format("Ignore Phase Completed, took {0}sec {1}msec", diff.Seconds, diff.Milliseconds);
            label1.Invoke(new SetlabelDelegate(SetLabelText));
        }


        private void buttonInitPhase_Click(object sender, EventArgs e)
        {
            SetAlgorithm mex1 = new SetAlgorithm();
            mex1.algorithmName = "PermutazioniLocali";
            object[] parameters = new object[2];
            parameters[0] = n;
            parameters[1] = b;
            mex1.algorithmParams = parameters;
            algManager.CallSetAlgorithm(mex1);

            labelText = "calling IgnorePhase...";
            SetLabelText();
            algManager.CallIgnorePhase();

        }

        private void buttonRunPhase_Click(object sender, EventArgs e)
        {
            labelText = "calling RunPhase...";
            SetLabelText();
            starTime = DateTime.Now;
            algManager.CallRunPhase();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        long n = 0;
        long b = 0;

        private string fileArray = @"PermutazioniLocaliArray";
        //private long nArray = (long) (1024L * 1024L * 1024L* 2L); // 2 GB
        private long nArray = (long)(1024L * 512L); // 2 GB
        private void button1_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            if (File.Exists(fileArray))
            {
                FileInfo fi = new FileInfo(fileArray);
                if (fi.Length >= nArray)
                {
                    return;
                }
            }
            MessageBox.Show("Inizio la generazione dell'array..");
            FileStream fs = File.OpenWrite(fileArray);
            for (long i = 0; i < nArray; i++)
            {
                fs.WriteByte((byte)rand.Next(byte.MinValue, byte.MaxValue));
            }
            fs.Close();
            MessageBox.Show("Generazione array terminata");

        }
    }
}
